#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->init();
}

Widget::~Widget()
{
    if (btnRes != NULL)
        delete btnRes;
    delete ui;
}

void Widget::init()
{
    this->setFixedSize(800, 200);
    editLeft = new QLineEdit("0", this);
    editRight = new QLineEdit("0", this);
    btnRes = new QPushButton("=", this);
    btnRes->show();
    cbx = new QComboBox(this);
    QStringList listOperator = {"+", "-", "x", "÷", "%"};
    cbx->addItems(listOperator);
    labelResult = new QLabel("结果", this);
    labelMessage = new QLabel("提示", this);

    editLeft->setGeometry(20, 20, 190, 70);
    cbx->setGeometry(220, 20, 70, 70);
    editRight->setGeometry(300, 20, 190, 70);
//    btnRes->setParent(this);
    btnRes->setGeometry(500, 20, 70, 70);
    labelResult->setGeometry(580, 20, 200, 70);
    labelMessage->setGeometry(20, 110, 760, 70);
    editLeft->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    editRight->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    labelResult->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    labelMessage->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    editLeft->setFont(QFont("yahei", 20));
    editRight->setFont(QFont("yahei", 20));
    cbx->setFont(QFont("yahei", 20));
    btnRes->setFont(QFont("yahei", 20));
    labelResult->setFont(QFont("yahei", 20));
    labelMessage->setFont(QFont("yahei", 20));

    editLeft->setStyleSheet("background-color:deeppink; color:#fff");
    editRight->setStyleSheet("background-color:deeppink; color:#fff");
    btnRes->setStyleSheet("background-color:deeppink; color:#fff");
    labelResult->setStyleSheet("background-color:deeppink; color:#fff");
    labelMessage->setStyleSheet("background-color:deeppink; color:#fff");

    string style=R"(
                 *{
                 font-size: 20px;
                 font-family:'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
                 }

                 QLabel
                 {
                 background-color: rgb(255, 85, 0);
                 color:#fff
                 }


                 QPushButton{
                 background-color:green;
                 color:#fff;
                 }

                 QLineEdit
                 {
                 background-color:deeppink;
                 color:#fff
                 }

                 )";

    qApp->setStyleSheet(style.data());

    connect(btnRes, SIGNAL(clicked()), this, SLOT(doRes()));
}

void Widget::doRes()
{
    qDebug("%s", __FUNCTION__);
    labelResult->setText("          ");
    labelMessage->setText("          ");
    QString textLeft = editLeft->text();
    QString textRight = editRight->text();
    if (textLeft.length() == 0)
    {
        labelMessage->setText("请输入操作数据1");
        editLeft->setFocus();
        return;
    }

    if (textRight.isEmpty())
    {
        labelMessage->setText("请输入操作数据2");
        editRight->setFocus();
        return;
    }

    double left, right;
    bool ok;
    left = textLeft.toDouble(&ok);
    if (!ok)
    {
        labelMessage->setText("操作数1不是纯数字");
        return;
    }

    right = textRight.toDouble(&ok);
    if (!ok)
    {
        labelMessage->setText("操作数2不是纯数字");
        return;
    }

    double res;
    switch (cbx->currentIndex()) {
    case 0:
        res = left + right;
        labelResult->setText(QString::number(res));
        break;
    case 1:
        res = left - right;
        labelResult->setText(QString::number(res));
        break;
    case 2:
        res = left * right;
        labelResult->setText(QString::number(res));
        break;
    case 3:
        res = left / right;
        if (right == 0)
        {
            labelMessage->setText("除数不能为0");
            return;
        }
        labelResult->setText(QString::number(res));
        break;
    case 4:
        int one, two;
        one = (int)left;
        editLeft->setText(QString::number(one));
        two = (int)right;
        editRight->setText(QString::number(two));
        if (two == 0)
        {
            labelMessage->setText("取模运算时，操作数2不能为0");
            return;
        }
        int res = one % two;
        //这个结果显示是刘思都加的
        labelMessage->setText("取模运算的结果是："+QString::number(res));
        labelResult->setText(QString::number(res));
        break;
    }
}
